网上有很多的教程,可以搜索【方志鹏】,他的教程很容易看懂,我写的目的只是为了记录 首先创建一个maven父工程,然后在父工程中创建一个springboot项目
4.0.0
<groupId>com</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
</plugin>
</plugins>
</build>
复制代码
pom.xml文件,然后在启动类上加上 @EnableEurekaServer注解
接下来就是修改配置文件 我用的是yml结尾的 server: port: 8761
eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: erueka-server
然后启动项目,启动成功之后再浏览器上访问 localhost:8761即可
接下来再创建一个springboot项目,取名叫eureka-client pom.xml文件如下
4.0.0
<groupId>com</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
复制代码
在启动类上加上注解 @EnableEurekaClient
然后修改配置文件
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
然后启动项目 接着在浏览器上访问localhost:8761 发现eureka-client已经注册了
然后eureka-client项目中新建一个接口 @RestController public class HelloController {
@GetMapping(value = "/hello")
public String hello(@RequestParam String name){
return "你好啊,"+name;
}
复制代码
}
然后再创建一个新的boot项目,pom.xml和eureka-client是一样的, 然后再启动类上加上注解 @EnableEurekaClient 我们使用RestTemplate来进行远程调用 在启动类中
@Bean @LoadBalanced private RestTemplate getRestTemplate(){ return new RestTemplate(); }
修改配置文件 server: port: 8763 spring: application: name: eureka-client-test eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
然后新建一个controller
@RestController public class TestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(@RequestParam String name){
String value = restTemplate.getForObject("http://eureka-client/hello?name="+name, String.class);
return value;
}
复制代码
} 将RestTemplate注入进来, 这里http://eureka-client/hello?name="+name name表示要传的参数,eureka-client就是你要调用的接口所在的服务名
将三个项目都启动,然后浏览器访问localhost:8763/test?name=张三 就可以了