一:创建eureka(注册中心)
1.file-----project
4.创建好项目后修改application.yml文件
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
5.编写启动类SpringbootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//该注解表明标注类是一个Eureka Server。
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
6.启动项目
在浏览器中输入http://localhost:8761/
7.创建生产者(创建方式与创建注册中心相同)
8.修改application.yml文件
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ server.port=8763 spring.application.name=service-hi
9.编写启动类
在application中加入注解@EnableEurekaClient,表明自己属于一个生产者。这里为了方便测试,直接使用@RestController获取返回值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringbootclientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name)
{
return "hi " + name + ",i am from port:" + port;
}
}
10.运行服务
在浏览器中输入http://localhost:8765/hi?name=fys,可以看到如下信息
11.创建消费者(创建方式与上面相同)
12.修改application.yml文件
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ server.port=8764 spring.application.name=service-ribbon
13.编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceribbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceribbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
}
@EnableDiscoveryClient表明标注类是消费者,加入restTemplate以消费相关的服务
14.创建service和controller
14.1 service层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name)
{
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
}
14.2 controller层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
这里利用字符串进行传输。当然restTemplate也是可以以对象进行传输的。
15.
.运行服务
在浏览器中输入http://localhost:8764/hi?name=admin