springCloud简单搭建

一:创建eureka(注册中心)

     1.file-----project

     

3.

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

消费者

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值