Springcloud+eureka Quick Start case one, service provider
Direct service, starter case there is no special place to set, note the lower port, because to start multiple services, may conflict
Configuration file (src/main/resources/application.yml)
server: port: 8000
Second, service consumers
The reliance of service consumers in this separate demo is actually dispensable, the pro-test does not add, you can also achieve the demo service to provide
Third, the service consumer startup class injects resttemplate for invoking remote services
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplicationpublic class MovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MovieApplication.class, args); }}
Iv. Service Consumer Controller
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import com.xujie.pojo.User;@RestControllerpublic class UserController { @Autowired private RestTemplate restTemplate; @GetMapping("/getUser") public User getUser() { return this.restTemplate.getForObject("http://localhost:8000/getUser", User.class); }}
At this point, the service provider's service can be indirectly invoked by accessing the consumer.
Create a service registry, where you choose Eureka5.1 to add dependencies on the Springboot base environment
<!-- springcloud版本声明 --><dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement> <!-- 引入eureka依赖 --><dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency></dependencies>
5.2 Encoding of the startup class
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServer //声明这是一个Eureka服务器public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5.3 Configuration file (SRC/MAIN/RESOURCES/APPLICATION.YML)
server: port: 8761 #声明端口号eureka: client: register-with-eureka: false #默认是true,将自己注册到eureka上 fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息 service-url: defaultZone: http://localhost:8761/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是:http://localhost:8761/eureka
Vi. registering the service provider with the Service Registry 6.1 retrofit service provider 6.1.1 add dependencies to facilitate the registration of services to the registry Eureka:
<!-- springcloud版本声明 --><dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement><!-- eureka的依赖 --><dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency></dependencies>
6.1.2 Modify the configuration file to add the following configuration
spring: application: name: provider #注册到Eureka Server上的应用名称eureka: client: service-url: defaultZone: http://localhost:8761/eureka #注册的位置 instance: prefer-ip-address: true #将自己的ip注册到EuekaServer上
6.1.3 Modifying the Startup class
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClient //也可以用EnableDiscoveryClient代替,前者兼容性更大,后者仅能兼容Eurekapublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
At this point, you can start the service normally and register the services in Eureka
* * Start Eureka and service provider access: http://localhost:8761, interface as follows: * *
Vii. High Availability of Eureka
* * Before the demo here, modify the local hosts file, in order to distinguish the local two Eureka nodes, respectively through: http://peer1:8761 and http://peer2:8762 access * *
7.1 Make a copy of the Eureka Project and modify the two configuration files as follows:
Configuration of the EUREKA1:
server: port: 8761 #声明端口号eureka: instance: hostname: peer1 appname: peer1 client: #register-with-eureka: false #默认是true,将自己注册到eureka上,这里设置eureka的高可用,所以需要将自己注册到eureka上 #fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息,这里设置eureka的高可用,所以需要在eureka上获取服务 service-url: defaultZone: http://peer2:8762/eureka
Configuration of the EUREKA2:
server: port: 8762 #声明端口号eureka: instance: hostname: peer2 appname: peer2 client: #register-with-eureka: false #默认是true,将自己注册到eureka上,这里设置eureka的高可用,所以需要将自己注册到eureka上 #fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息,这里设置eureka的高可用,所以需要在eureka上获取服务 service-url: defaultZone: http://peer1:8761/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是:http://localhost:8761/eureka
Two services are started at this time, the interface is as follows
This is peer1:
Here's the Peer2:
Springcloud+eureka Simple Getting Started case