1. Eureka
Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。
步骤:
- 搭建 Provider 和 Consumer 服务。
父工程pom配置文件,一下工程均在此父工程下搭建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.iteima</groupId>
<artifactId>springcloud-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloudeurekaprovide</module>
<module>springcloudeurekaconsumer</module>
<module>springcloudeurekaserver</module>
<module>springcloudeurekaserver01</module>
<module>springcloudeurekaserver02</module>
<module>consul_provider</module>
<module>consul_consumer</module>
<module>nacosconsumer</module>
<module>nacosprovider</module>
</modules>
<!--springboot环境-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- provider
provider的pom配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-parent</artifactId>
<groupId>com.iteima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudeurekaprovide</artifactId>
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
引导类,需添加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
yml配置文件
server:
port: 8000
#eureka配置
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 9
client:
service-url:
defaultZone: http://eureka-server2:8763/eureka,http://eureka-server1:8762/eureka
register-with-eureka: true
fetch-registry: true
spring:
application:
name: eureka-provide
还需添加项目controller层、service层、dao层业务逻辑代码,此处省略。
- consumer
pom文件与provider中的pom文件一样
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-parent</artifactId>
<groupId>com.iteima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudeurekaconsumer</artifactId>
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
cosumer中的启动类需添加@EnableEurekaClient、@EnableDiscoveryClient 两个注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //激活DiscoveryClient
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
编写配置类,因需要使用 RestTemplate 完成远程调用。
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Consumer 服务 通过从 Eureka Server 中抓取 Provider地址 完成 远程调用
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
List<ServiceInstance> instances = discoverClient.getInstances("EUREKA-PROVIDE");
if(instances==null || instances.size()==0){
return null;
}
ServiceInstance serviceInstance = instances.get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url="http://"+host+":"+port+"goods/findone/"+id;
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}
yml配置文件
server:
port: 9000
#eureka配置
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://eureka-server2:8763/eureka,http://eureka-server1:8762/eureka
register-with-eureka: true
fetch-registry: true
spring:
application:
name: eureka-consumer
- 搭建 Eureka Server 服务。
pom配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-parent</artifactId>
<groupId>com.iteima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudeurekaserver</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
Eureka Server配置文件
server:
port: 8761
#eureka配置
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 3000
Eureka Server启动类,添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
2.Consul
consul的使用与Eureka类似,需在power shell中启动服务,命令为
./consul.exe agent -dev
即可登录loalhost:8500进入控制台
搭建provide和consumer工程即可
- provide
pom文件,在springcloudparent父工程下搭建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-parent</artifactId>
<groupId>com.iteima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consul_provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
</project>
引导类不需要添加其它注解,业务工程与eureka一致,yaml配置文件为
server:
port: 8000
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
application:
name: consul-provider
- consumer
consumer中其它文件与eureka中一致,其中不同的为引导类,服务调用文件,配置文件
引导类,只需添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsulConsumerApp.class,args);
}
}
Consumer 服务抓取 Provider地址 完成 远程调用即可
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
List<ServiceInstance> instances = discoveryClient.getInstances("consul-provider");
if(instances==null || instances.size()==0){
return null;
}
ServiceInstance serviceInstance = instances.get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url="http://"+host+":"+port+"goods/findone/"+id;
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}
yml配置文件
server:
port: 9000
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
application:
name: consul-consumer
3.nacos
双击启动\nacos\bin目录下的startup.cmd文件,浏览器进入http://localhost:8848/nacos,输入账号密码登录即可,默认账号密码均为nacos
nacos搭建工程与Consul几乎一致,不同的是配置文件和坐标导入文件
- provider
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-parent</artifactId>
<groupId>com.iteima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacosprovider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
yml配置文件
server:
port: 8000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: nacos-provide
- consumer
pom配置文件与provider一样,yml配置文件
server:
port: 9000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: nacos-consumer
小结
SpringCloud中使用三种注册中心的步骤大同小异,springCloud为我们进行了封装,在调用注册中心时只需要导入正确的坐标,修改配置文件即可,Consumer 服务抓取Provider地址完成远程调用即可。三者不同的是服务中心的启动。